package com.security.jwt.jwtserver.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * created by wuquan at 05/19/2021
 * ^_^ ^_^
 */
@RestController
public class LoginController {


    @RequestMapping("/index")

    public String login2() {

        return "index";
    }

    //@RequestMapping("/error")
    public String error() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();

        return "error";
    }

    @RequestMapping("/r/r3")
    @PreAuthorize("hasAnyAuthority('r0')")
    public String r3() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(JSONObject.toJSONString(authentication));


        return "r3";
    }


    @RequestMapping("/r/r1")
    public String r1() {

        return "r1";
    }

    @RequestMapping("/r/r2")
    public String r2() {

        return "r2";
    }

}
